--
-- Author: shunguo.chen
-- Date: 2016-11-08 10:15:50
--
-- IMBubbleRender

local ClassRef = app.mvc.createRenderClass(...)

-- data = IMMessage
function ClassRef.create(node)
	local sprite = ClassRef.extend(cc.CSLoader:createNode(Res.IMBubbleNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)
	-- print("IMBubbleRender.init()")

	self:setContentSize(self._ui.Image_1:getContentSize())
	self._ui.Text:setVisible(false)
	self._ui.con:setClippingEnabled(false)

	self._size = self:getContentSize()
	self._conSize = self._ui.con:getContentSize()

	self._ui.Button:addClickEventListener(function()
		-- print("IMBubbleRender click")
		executeFunc(self._onClick, self)
	end)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		-- 只有招募信息需要响应点击
		self._ui.Button:setTouchEnabled(self._data:isTeamreqMsg())
		self:setContent(self._data:getPpTextXml())
	end
end

function ClassRef:play()
	display.playTimeline(self)
end

function ClassRef:stopPlay()
	display.pauseTimeline(self, "max")
end

function ClassRef:faceTo(scale)
	if scale > 0 then
		self:setScaleX(1)
		self._ui.con:setScaleX(1)
	elseif scale < 0 then
		self:setScaleX(-1)
		self._ui.con:setScaleX(-1)
	end
end

function ClassRef:setContent(content)
	if self._textRT ~= nil then
		self._textRT:removeFromParent()
		self._textRT = nil
	end

	local defaults = {}
	-- defaults["KEY_WRAP_MODE"] = 1
	--defaults["KEY_FONT_FACE"] = Res.font
	defaults["KEY_FONT_SIZE"] = self._ui.Text:getFontSize()
	defaults["KEY_VERTICAL_SPACE"] = 2

	local richText = ccui.RichText:createWithXML(content, defaults)
	richText:setEnabled(false)
	richText:setTouchEnabled(false)
	richText:setAnchorPoint(cc.p(0, 0))
	richText:ignoreContentAdaptWithSize(false)
	self._ui.con:addChild(richText, 2)
	self._textRT = richText

	richText:setContentSize(cc.size(self._conSize.width, 0))
	richText:formatText()
	local t_size = richText:getFormatSize()
	richText:setContentSize(t_size)
	richText:setOpenUrlHandler(function() end)

	local bgHeight = math.max(t_size.height, self._conSize.height)
	-- print("IMBubbleRender.bgHeight = " .. bgHeight)

	local newContSize = cc.size(self._size.width, bgHeight - self._conSize.height + self._size.height)
	self:setContentSize(newContSize)
	self._ui.Image_1:setContentSize(newContSize)

	ccui.Helper:doLayout(self)
end

function ClassRef:onBtnClick(func)
	self._onClick = func
end

return ClassRef